﻿using System;
using System.Linq;
using MongoDB.Driver;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Synchronization.Validation.Internals;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;
using Xunit;

namespace Sustainalytics.Synchronization.Validation.Tests
{
    public class AllRulesFromAssemblyTest
    {
        [Fact]
        public void InsertInDatabase()
        {
            var validationCollection =
                MongoFactory.GetCollectionFromDatabase<RuleMetadata>("mongodb://localhost:27017/Validation", "Validators");
            validationCollection.DeleteMany(FilterDefinition<RuleMetadata>.Empty);

            var assembly = typeof(Rule<>).Assembly;

            var types = assembly.GetTypes().Where(t => t.IsSubclassOfRawGeneric(typeof(Rule<>)));
            foreach (var type in types)
            {
                Type constructedType = typeof(FullNameDiscriminatorClassMap<>).MakeGenericType(type);
                Activator.CreateInstance(constructedType, null);
                validationCollection.InsertOne((RuleMetadata)Activator.CreateInstance(type, null));
            }
        }
    }
}